from IPython.display import Image
Image("img/add_multiply.png")
Therefore the scenarios could be:
from IPython.display import Image
Image("img/simple_exponential_smoothing.png")
Weights as a function of alpha
from IPython.display import Image
Image("img/simple_exponential_forcast2.png",width=700)
Image("img/simple_exponential_forcast.png")
Image("img/forcast_simple_exp.png")
Image("img/holt_linear.png")
Image("img/holt_linear2.png")
The forecasts generated by Holt’s linear method display a constant trend (increasing or decreasing) indefinitely into the future. Empirical evidence indicates that these methods tend to over-forecast, especially for longer forecast horizons. Motivated by this observation, Gardner & McKenzie (1985) introduced a parameter that “dampens” the trend to a flat line some time in the future.
Image("img/holt_linear_damped.png")
Image("img/forcast_holt_damp.png")
Multiplicative seasonal should have varying oscillation
Image("img/comparison_methods.png")
Image("img/holt_winter.png")
Image("img/summary_holt_damp_winter2.png")
Image("img/summary_holt_damp_winter1.png")
import statsmodels.api as sm
from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt
import pandas as pd
df = pd.read_excel("champagne-sales.xlsx")
df.head()
df["Champagne Sales"].values
def concat_funct(a, b):
return a+'-'+b+'-01'
df['year'] = df['Month'].apply(lambda x:'200'+x[0])
df['month'] = df['Month'].apply(lambda x:x[-2:])
df['full-year-month'] = df.apply(lambda x:str(concat_funct(x.year, x.month)), axis=1)
df['full-year-month'] = pd.to_datetime(df['full-year-month'])
df.info()
df.head()
New_df = pd.DataFrame(df["Champagne Sales"].values, index=df['full-year-month'].values, columns=['Champagne_Sales'])
New_df.tail(20)
New_df.shape[0]
import matplotlib.pyplot as plt
plt.figure(figsize=(20,10))
decomposition = sm.tsa.seasonal_decompose(New_df["Champagne_Sales"], model='additive')
fig = decomposition.plot()
plt.show()
train = New_df.loc['2001-01-01':'2007-12-01']
test = New_df.loc['2007-12-01':]
import numpy as np
fit_holt = Holt(np.asarray(train['Champagne_Sales']), exponential=False, damped=True).fit(optimized=True)
y_predict = test.copy()
y_predict['Holt'] = fit_holt.forecast(len(test))
y_predict
fit_holt_exp = Holt(np.asarray(train['Champagne_Sales']), exponential=True, damped=True).fit()
y_predict['Holt_exp'] = fit_holt_exp.forecast(len(test))
y_predict
from statsmodels.tsa.holtwinters import ExponentialSmoothing
model = ExponentialSmoothing(np.asarray(train['Champagne_Sales']), trend="add", seasonal="add", seasonal_periods=12)
model2 = ExponentialSmoothing(np.asarray(train['Champagne_Sales']), trend="add", seasonal="add", seasonal_periods=12, damped=True)
model3 = ExponentialSmoothing(np.asarray(train['Champagne_Sales']), trend="add", seasonal="mul", seasonal_periods=12, damped=True)
fit = model.fit()
y_predict['Holt_winter_aa'] = fit.forecast(len(test))
fit2 = model2.fit()
y_predict['Holt_winter_aad'] = fit2.forecast(len(test))
fit3 = model3.fit()
y_predict['Holt_winter_amd'] = fit3.forecast(len(test))
y_predict
plt.figure(figsize=(20,10))
plt.plot(New_df.index, New_df['Champagne_Sales'])
#plt.plot(y_predict.index,y_predict['Champagne_Sales'])
plt.plot(y_predict.index,y_predict['Holt'], label='Holt_linear')
plt.plot(y_predict.index,y_predict['Holt_exp'], label='Holt_exp')
plt.plot(y_predict.index,y_predict['Holt_winter_aa'], label='Holt_winter_trend-A_season-A')
plt.plot(y_predict.index,y_predict['Holt_winter_aad'], label='Holt_winter_trend-A_season-A_damp')
plt.plot(y_predict.index,y_predict['Holt_winter_amd'], label='Holt_winter_trend-A_season-M_damp')
plt.legend()
model0 = SimpleExpSmoothing(np.asarray(train['Champagne_Sales']))
fit0 = model0.fit()
y_predict['Holt_simple'] = fit0.forecast(len(test))
model00 = ExponentialSmoothing(np.asarray(train['Champagne_Sales']),trend='add', seasonal=False, seasonal_periods=None, damped=False)
fit00 = model00.fit()
y_predict['Holt_linear'] = fit00.forecast(len(test))
plt.figure(figsize=(20,10))
plt.plot(New_df.index, New_df['Champagne_Sales'])
plt.plot(y_predict.index,y_predict['Holt_simple'], label='Holt_constant')
plt.plot(y_predict.index,y_predict['Holt_linear'], label='Holt_linear')
plt.plot(y_predict.index,y_predict['Holt_exp'], label='Holt_exp')
plt.plot(y_predict.index,y_predict['Holt_winter_aa'], label='Holt_winter_trend-A_season-A')
plt.plot(y_predict.index,y_predict['Holt_winter_aad'], label='Holt_winter_trend-A_season-A_damp')
plt.plot(y_predict.index,y_predict['Holt_winter_amd'], label='Holt_winter_trend-A_season-M_damp')
plt.legend()